<?php

namespace common\components;

use bedezign\yii2\audit\Audit;
use common\behaviors\ModelAuditBehavior;
use Yii;
use yii\db\ActiveRecord as BaseActiveRecord;

/**
 * Base active record class
 *
 * @package common\components
 */
class ActiveRecord extends BaseActiveRecord
{

    /**
     * @const  开启
     */
    const STATUS_OFF = 0;

    /**
     * @const 关闭
     */
    const STATUS_ON  = 1;

    /**
     * @return array
     */
    public static function statusArray()
    {
        return [
            self::STATUS_ON => Yii::t('common', 'On'),
            self::STATUS_OFF => Yii::t('common', 'Off'),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function find()
    {
        return new ActiveQuery(get_called_class());
    }

    /**
     * {@inheritdoc}
     */
    public static function getDb()
    {
        return Yii::$app->get('db');
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        if (Audit::getInstance() === null)
        {
            return [];
        }
        else
        {
            return [
                ModelAuditBehavior::className(),
            ];
        }
    }

    /**
     * 将错误信息格式化为一个字符串
     *
     * @return string
     */
    public function formatErrors()
    {
        $result = '';
        foreach ($this->getErrors() as $attribute => $errors)
        {
            $result .= implode(" ", $errors) . " ";
        }
        return $result;
    }
}
